/**
 * Created by Administrator on 2017/8/23 0023.
 */
var gridLayer;
var addGridLayer = false;
var gridItems = grids.items;

function addGridLayerFun(){
    if( !gridItems || gridItems.length<1){
        alert('no grids are found!');
        return
    }
    if (!addGridLayer){
        var geojsonObject = {
            'type': 'FeatureCollection',
            'crs': {
                'type': 'name',
                'properties': {
                    'name': 'EPSG:4326'
                }
            },
            'features': [ {
                'type': 'Feature',
                'geometry': {
                    'type': 'Polygon',
                    'coordinates':[[[113.5082512010734,23.76968073642652],[113.6082512010734,23.86968073642652]
                    ]]
                }
            }]
        };
        for(i = 0,size = gridItems.length;i<size;i++){
            var lon = gridItems[i].LONGITUDE;
            var lat = gridItems[i].LATITUDE;
            var lonStep = gridItems[i].LONGITUDE_STEP;
            var latStep = gridItems[i].LATITUDE_STEP;
            var grid = [];
            var feature ;
            if(i%2 ==0){
                feature ={
                    'type': 'Feature',
                    "properties": {
                        color: 'green'
                    },
                    'geometry': {
                        'type': 'Polygon',
                        'coordinates':[[[lon,lat],[lon+lonStep,lat],[lon+lonStep,lat+latStep],[lon,lat+latStep]
                        ]]
                    }
                };

            }else{
                feature ={
                    'type': 'Feature',
//                        "properties": {
//                            color: 'blue'
//                        },
                    'geometry': {
                        'type': 'Polygon',
                        'coordinates':[[[lon,lat],[lon+lonStep,lat],[lon+lonStep,lat+latStep],[lon,lat+latStep]
                        ]]
                    }
                };
            }
            geojsonObject.features.push(feature);
            //for(var j=0;j<6;j++){
                geojsonObject.features.push(feature);
            //}
//                if(i>0)
//                break;
        }
        var source = new ol.source.Vector({
            features: (new ol.format.GeoJSON()).readFeatures(geojsonObject,{dataProjection:"EPSG:4326",featureProjection:"EPSG:3857"})
        });
        console.log("total grids "+geojsonObject.features.length);
        gridLayer = new ol.layer.Vector({
            source: source,
            style: styles
        });

        map.addLayer(gridLayer);

    }else{
        if(gridLayer){
            map.removeLayer(gridLayer);
        }
    }
    addGridLayer = !addGridLayer;
}

